package cn.dw.web.action;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import cn.dw.domain.CheckInfo;
import cn.dw.domain.People;
import cn.dw.domain.Point;
import cn.dw.service.CheckInfoService;

public class CheckInfoAction
  extends ActionSupport
  implements ModelDriven<CheckInfo>
{
  private static final long serialVersionUID = -4110952865784469569L;
  private CheckInfo checkInfo = new CheckInfo();
  private CheckInfoService cis;
  private CheckInfo cInfo = new CheckInfo();
  private People peo = new People();
  private Point point = new Point();
  private HashMap<String, Object> map = null;
  private ChangetoJson ctj = new ChangetoJson();
  private SimpleDateFormat day = new SimpleDateFormat("yyyy-MM-dd");
  private Calendar calendar = Calendar.getInstance();
  private Date ddday;
  private String today;
  
  public void calender()
  {
    this.calendar.set(2017, 4, 10);
    this.ddday = this.calendar.getTime();
    this.today = this.day.format(this.ddday);
  }
  
  private IfState is = new IfState();
  
  public String list()
    throws Exception
  {
    List<CheckInfo> checkInfos = this.cis.getAll();
    
    List<Map<String, Object>> list = new ArrayList<>();
    
    HttpServletRequest request = ServletActionContext.getRequest();
    
    request.setCharacterEncoding("utf-8");
    
    SimpleDateFormat day = new SimpleDateFormat("yyyy-MM-dd");
    
    Date stime = day.parse(request.getParameter("stime"));
    Date etime = day.parse(request.getParameter("etime"));
    if (checkInfos.size() > 0) {
      for (int i = 0; i < checkInfos.size(); i++)
      {
        this.cInfo = ((CheckInfo)checkInfos.get(i));
        this.peo = this.cInfo.getPeople();
        this.point = this.peo.getPoint();
        
        this.map = new HashMap<>();
        if ((this.cInfo.getDate().after(stime)) && (this.cInfo.getDate().before(etime)))
        {
          String state;
          if (!this.cInfo.isReply()) {
            state = "异常";
          } else {
            state = this.is.State(this.point.getLng(), this.point.getLat(), 
              this.cInfo.getLongitude(), this.cInfo.getLatitude(), 
              this.cInfo.getDate(), this.cInfo.getReplydate());
          }
          this.map.put("date", this.cInfo.getDate().toString());
          this.map.put("addr", this.point.getAddr());
          this.map.put("people", this.peo.getName());
          this.map.put("phone", this.peo.getPhone());
          this.map.put("state", state);
          
          list.add(this.map);
        }
      }
    }
    this.ctj.json(list);
    
    return null;
  }
  
  public String todaynormal()
    throws Exception
  {
    calender();
    
    List<CheckInfo> checkInfos = this.cis.getAll();
    
    String date1 = null;
    
    List<Map<String, Object>> list = new ArrayList<>();
    if (checkInfos.size() > 0) {
      for (int i = 0; i < checkInfos.size(); i++)
      {
        this.cInfo = ((CheckInfo)checkInfos.get(i));
        this.peo = this.cInfo.getPeople();
        this.point = this.peo.getPoint();
        
        this.day = new SimpleDateFormat("yyyy-MM-dd");
        date1 = this.day.format(this.cInfo.getDate());
        if (date1.equals(this.today))
        {
          this.map = new HashMap<>();
          String state;
          if (!this.cInfo.isReply()) {
            state = "异常";
          } else {
            state = this.is.State(this.point.getLng(), this.point.getLat(), 
              this.cInfo.getLongitude(), this.cInfo.getLatitude(), 
              this.cInfo.getDate(), this.cInfo.getReplydate());
          }
          if (state.equals("正常"))
          {
            this.map.put("date", this.cInfo.getDate().toString());
            this.map.put("addr", this.point.getAddr());
            this.map.put("state", state);
            this.map.put("lng", this.point.getLng());
            this.map.put("lat", this.point.getLat());
            
            list.add(this.map);
          }
        }
      }
    }
    this.ctj.json(list);
    
    return null;
  }
  
  public String todayabnormal()
    throws Exception
  {
    calender();
    List<CheckInfo> checkInfos = this.cis.getAll();
    String date1 = null;
    
    List<Map<String, Object>> list = new ArrayList<>();
    if (checkInfos.size() > 0) {
      for (int i = 0; i < checkInfos.size(); i++)
      {
        this.cInfo = ((CheckInfo)checkInfos.get(i));
        this.peo = this.cInfo.getPeople();
        this.point = this.peo.getPoint();
        this.day = new SimpleDateFormat("yyyy-MM-dd");
        date1 = this.day.format(this.cInfo.getDate());
        if (date1.equals(this.today))
        {
          this.map = new HashMap<>();
          String state;
          if (!this.cInfo.isReply()) {
            state = "异常";
          } else {
            state = this.is.State(this.point.getLng(), this.point.getLat(), 
              this.cInfo.getLongitude(), this.cInfo.getLatitude(), 
              this.cInfo.getDate(), this.cInfo.getReplydate());
          }
          if (state.equals("异常"))
          {
            this.map.put("date", this.cInfo.getDate().toString());
            this.map.put("addr", this.point.getAddr());
            this.map.put("state", state);
            this.map.put("lng", this.point.getLng());
            this.map.put("lat", this.point.getLat());
            
            list.add(this.map);
          }
        }
      }
    }
    this.ctj.json(list);
    return null;
  }
  
  public String abnormal3()
    throws Exception
  {
    ifnormal(3, 3, "异常");
    
    return null;
  }
  
  public String abnormal6()
    throws Exception
  {
    ifnormal(6, 6, "异常");
    
    return null;
  }
  
  public String normal30()
    throws Exception
  {
    ifnormal(30, 30, "正常");
    
    return null;
  }
  
  public String abnormal30of6()
    throws Exception
  {
    ifnormal(30, 6, "异常");
    
    return null;
  }
  
  public String ifnormal(int ii, int iii, String State)
    throws Exception
  {
    List<CheckInfo> checkInfos = this.cis.getAll();
    
    calender();
    
    String date1 = null;
    
    Calendar cal = Calendar.getInstance();
    List<CheckInfo> cList = new ArrayList<>();
    
    List<Map<String, Object>> list = new ArrayList<>();
    if (checkInfos.size() > 0) {
      for (int i = 0; i < checkInfos.size(); i++)
      {
        this.cInfo = new CheckInfo();
        this.cInfo = ((CheckInfo)checkInfos.get(i));
        this.peo = this.cInfo.getPeople();
        this.point = this.peo.getPoint();
        cal.setTime(this.ddday);
        
        date1 = this.day.format(this.cInfo.getDate());
        String state;
        if (!this.cInfo.isReply()) {
          state = "异常";
        } else {
          state = this.is.State(this.point.getLng(), this.point.getLat(), 
            this.cInfo.getLongitude(), this.cInfo.getLatitude(), 
            this.cInfo.getDate(), this.cInfo.getReplydate());
        }
        for (int a = 0; a < ii; a++)
        {
          Date date2 = cal.getTime();
          String dday = this.day.format(date2);
          if ((date1.equals(dday)) && 
            (state.equals(State))) {
            cList.add((CheckInfo)checkInfos.get(i));
          }
          cal.add(5, -1);
        }
      }
    }
    List<CheckInfo> ccInfos = new ArrayList<>();
    for (int n = 0; n < cList.size(); n++) {
      ccInfos.add((CheckInfo)cList.get(n));
    }
    List<CheckInfo> cccInfos = new ArrayList<>();
    if (cList.size() > 0) {
      for (int i = 0; i < cList.size(); i++)
      {
        int count = 1;
        this.cInfo = new CheckInfo();
        this.cInfo = ((CheckInfo)cList.get(i));
        this.peo = this.cInfo.getPeople();
        this.point = this.peo.getPoint();
        for (int j = i + 1; j < cList.size(); j++)
        {
          CheckInfo ciInfo = new CheckInfo();
          ciInfo = (CheckInfo)cList.get(j);
          People p = new People();
          Point po = new Point();
          p = ciInfo.getPeople();
          po = p.getPoint();
          if (po.getId() == this.point.getId())
          {
            count++;
            cList.remove(j);
            j--;
          }
        }
        if (count >= iii) {
          cccInfos.add((CheckInfo)cList.get(i));
        }
        count = 1;
      }
    }
    if (cccInfos.size() > 0) {
      for (int i = 0; i < cccInfos.size(); i++)
      {
        this.cInfo = new CheckInfo();
        this.cInfo = ((CheckInfo)cccInfos.get(i));
        this.peo = this.cInfo.getPeople();
        this.point = this.peo.getPoint();
        if (ccInfos.size() > 0) {
          for (int j = 0; j < ccInfos.size(); j++)
          {
            CheckInfo ciInfo = new CheckInfo();
            ciInfo = (CheckInfo)ccInfos.get(j);
            People p = new People();
            Point po = new Point();
            p = ciInfo.getPeople();
            po = p.getPoint();
            if (po.getId() == this.point.getId())
            {
              this.map = new HashMap<>();
              this.map.put("date", ciInfo.getDate().toString());
              this.map.put("addr", po.getAddr());
              this.map.put("state", State);
              this.map.put("lng", this.point.getLng());
              this.map.put("lat", this.point.getLat());
              
              list.add(this.map);
            }
          }
        }
      }
    }
    this.ctj.json(list);
    
    return null;
  }
  
  public void setCis(CheckInfoService cis)
  {
    this.cis = cis;
  }
  
  public CheckInfo getModel()
  {
    return this.checkInfo;
  }
}
